//
//  RHPlantGroupConfigVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/2/20.
//

#import "RHBatchPlantGroupConfigVC.h"
#import "RHGolobalSetModel.h"
#import "RHVppGroupSetView.h"
#import "RHBatchPlantGroupSendView.h"
#import "RHBatch233VPPConfigVC.h"
#import "RHBatchS40KVPPConfigVC.h"
#import "RHBatchSolarkVPPConfigVC.h"
#import "RHBatchHiSolarkVPPConfigVC.h"

@interface RHBatchPlantGroupConfigVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *dataArr;



@end

@implementation RHBatchPlantGroupConfigVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"设置".rh_localized;
    [self setupUI];
}

-(void)setupUI
{
    self.dataArr =@[
                   @{
                        CellTitle:@"批量强制充电".rh_localized,
                        CellIcon:@"plant_group_batch_chg",
                        CellAction:@"chgAction"
                    },
                   @{
                        CellTitle:@"批量强制放电".rh_localized,
                        CellIcon:@"plant_group_batch_dchg",
                        CellAction:@"dischgAction"
                    },
                   @{
                        CellTitle:@"批量待机".rh_localized,
                        CellIcon:@"plant_group_batch_standby",
                        CellAction:@"standbyAction"
                    },
                   @{
                        CellTitle:@"批量下发策略".rh_localized,
                        CellIcon:@"plant_group_batch_policy",
                        CellAction:@"policyConfigAction"
                    },
                   @{
                        CellTitle:@"强制充放电配置".rh_localized,
                        CellIcon:@"plant_group_batch_config",
                        CellAction:@"chgDischgConfigAction"
                    },
                   @{
                        CellTitle:@"KeHua-120KW",
                        CellIcon:@"plant_group_batch_policy_config",
                        CellAction:@"commonPolicyConfigAction"
                    },
                   @{
                        CellTitle:@"MegarevoLv-12kW",
                        CellIcon:@"plant_group_batch_policy_config",
                        CellAction:@"s40KPolicyConfigAction"
                    },
                   @{
                        CellTitle:@"Sol-Ark-30kW",
                        CellIcon:@"plant_group_batch_policy_config",
                        CellAction:@"solark30kPolicyConfigAction"
                    },
                   @{
                        CellTitle:@"Sol-Ark-60kW",
                        CellIcon:@"plant_group_batch_policy_config",
                        CellAction:@"solark60kPolicyConfigAction"
                    },
                   @{
                       CellTitle:@"XStackHv-15KW",
                       CellIcon:@"plant_group_batch_policy_config",
                       CellAction:@"HISolarkPolicyConfigAction"
                    }
                   ];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setSeparatorColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    _tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

    
//    if (@available(iOS 11.0, *)) {
//        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dataDic = self.dataArr[indexPath.row];
    NSString *title = dataDic[CellTitle];
    NSString *iconStr = dataDic[CellIcon];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    [cell.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(8, 15, 8, 15));
    }];
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:iconStr]];
    icon.contentMode = UIViewContentModeScaleAspectFit;
    [bgView addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.width.height.offset(35);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = title;
    titleLab.font = kSYSTEMFONT(15);
    titleLab.textColor = kCOLOR_HEX(@"3F4443");
    titleLab.numberOfLines= 2;
    [bgView addSubview:titleLab];
    

    UIImageView *accesory = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    accesory.contentMode = UIViewContentModeScaleAspectFit;
    [bgView addSubview:accesory];
    [accesory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(10);
        make.right.equalTo(accesory.mas_left).offset(-5);
        make.centerY.equalTo(icon);
    }];
    
    cell.contentView.backgroundColor = CFTabViewBgColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dataDic = self.dataArr[indexPath.row];
    SEL sel = NSSelectorFromString(dataDic[CellAction]);
    if ([self respondsToSelector:sel]) {
        [self performSelector:sel];
    }
}

#pragma mark ------ 批量强制充电
-(void)chgAction
{
    RHBatchPlantGroupSendView *batSendView = [[RHBatchPlantGroupSendView alloc]initWithType:1];
    [batSendView show];
}

#pragma mark ------ 批量强制放电
-(void)dischgAction
{
    RHBatchPlantGroupSendView *batSendView = [[RHBatchPlantGroupSendView alloc]initWithType:2];
    [batSendView show];
}

#pragma mark ------ 批量待机
-(void)standbyAction
{
    RHBatchPlantGroupSendView *batSendView = [[RHBatchPlantGroupSendView alloc]initWithType:3];
    [batSendView show];
}

#pragma mark ------ 批量下发策略
-(void)policyConfigAction
{
    RHBatchPlantGroupSendView *batSendView = [[RHBatchPlantGroupSendView alloc]initWithType:4];
    [batSendView show];
}

#pragma mark ------ 强制充放电配置
-(void)chgDischgConfigAction
{
    [self getplantGroupGolobalConfig];
}

#pragma mark ------ 批量策略配置（通用）
-(void)commonPolicyConfigAction
{
    RHBatch233VPPConfigVC *configVC = [[RHBatch233VPPConfigVC alloc]init];
    [self.navigationController pushViewController:configVC animated:YES];
}

#pragma mark ------ 批量策略配置（S40K）
-(void)s40KPolicyConfigAction
{
    RHBatchS40KVPPConfigVC *configVC = [[RHBatchS40KVPPConfigVC alloc]init];
    [self.navigationController pushViewController:configVC animated:YES];
}

#pragma mark ------ 批量策略配置（Solark30k）
-(void)solark30kPolicyConfigAction
{
    RHBatchSolarkVPPConfigVC *configVC = [[RHBatchSolarkVPPConfigVC alloc]initWithInvType:InvType_Sol_Ark_30K_3P_208V];
    [self.navigationController pushViewController:configVC animated:YES];
}
#pragma mark ------ 批量策略配置（Solark60k）
-(void)solark60kPolicyConfigAction
{
    RHBatchSolarkVPPConfigVC *configVC = [[RHBatchSolarkVPPConfigVC alloc]initWithInvType:InvType_Sol_Ark_60K_3P_480V];
    [self.navigationController pushViewController:configVC animated:YES];
}
#pragma mark ------ 批量策略配置（HISolark）
-(void)HISolarkPolicyConfigAction
{
    RHBatchHiSolarkVPPConfigVC *configVC = [[RHBatchHiSolarkVPPConfigVC alloc]init];
    [self.navigationController pushViewController:configVC animated:YES];
}

#pragma mark ----- 获取一键充放电参数配置
-(void)getplantGroupGolobalConfig{
    [QMUITips showLoadingInView:kAppWindow];
    [RHGolobalSetModel startGetWithCompletionAPIKey:H_getPlantGroupForceConfig argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
//            self.vppConfigModel = resultModel.data;
            NSMutableArray *mtbArr = [NSMutableArray array];
            if (request.responseJSONObject[@"data"]) {
                NSDictionary *dic = request.responseJSONObject[@"data"];
                NSArray *allConfigs = dic.allValues;
                
                [allConfigs enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
                    RHGolobalSetModel *configModel = [RHGolobalSetModel yy_modelWithJSON:config];
                    if (configModel) {
                        [mtbArr addObject:configModel];
                    }
                }];
            }
            RHVppGroupSetView *setView = [[RHVppGroupSetView alloc]initWithConfig:mtbArr.copy];
 
            [setView show];
        }
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
        }];
    
}
@end
